import csv

if __name__ == '__main__':

    file = "D:\\combined.csv"
    file_iter = open(file, 'rU')
    
    distinctDoms = dict()
    access_count={}

    print 'Begin'
    for line in file_iter:
        line = line.strip().rstrip(',')
        eleme = line.split(',')
        for elem in eleme:
            if distinctDoms.has_key(elem):
                count = int(distinctDoms.get(elem))
                count += 1
                distinctDoms[elem] = count
            else:
                distinctDoms[elem] = 1

    list =  sorted(distinctDoms.items(), key=lambda x: x[1], reverse=True)
    
    n=0
    f = open("D:\\distinct_doms.txt", 'w')
    myfile_1 = open('D:\\stat1.txt', 'wb')
    myfile_2 = open('D:\\stat2.txt', 'wb')
#    wr_1 = csv.writer(myfile_1)
#    wr_2 = csv.writer(myfile_2)

    for a, b in list:
        if access_count.has_key(int(b)):
            count = int(access_count.get(b))
            count += 1
            print count
            access_count[b] = count
        else:
            access_count[b] = 1
        
        f.write(str(a) + ',' + str(b) + '\n')
        
    for key in access_count.keys():
        print "key",key
        myfile_1.writelines((str(key)+'\n'))
        myfile_2.writelines((str(access_count.get(key))+'\n'))
    print len(list), 'items processed.'    
    print 'End'    

                
        
    
    